﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;

namespace Actuals.Data
{
    public class DalSubTask : Dal<ISubTask, SubTask>, IDalSubTask
    {
           private readonly IDalManager _manager;
        private static readonly IDalSubTask _instance = new DalSubTask();
        private const string SP_GET_SUB_TASKS = "usp_GetSubTasks";
        private const string SP_GET_SUB_TASK = "usp_GetSubTask";

        public static IDalSubTask Instance
        {
            get { return _instance; }
        }

        public DalSubTask()
        {
            _manager = new DalManager();
        }

        public DalSubTask(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ISubTask obj, DataRow row)
        {
          
            obj.TaskId = _manager.GetValue<int>(row, "TaskId");
            obj.SubTaskId = _manager.GetValue<int>(row, "SubTaskId");
            obj.Description = _manager.GetValue<string>(row, "Description");

        }
        public IList<ISubTask> GetSubTasks(int taskId)
        {
            _manager.StoredProcedureName = SP_GET_SUB_TASKS;
            _manager.AddInParameter("@TaskId", DbType.Int32 , taskId);
          

            List<ISubTask> subTasks = new List<ISubTask>();
            foreach (ISubTask subTask in ConvertAll(_manager.ExecuteDataSet()))
                subTasks.Add(subTask);

            return subTasks;
        }


        public ISubTask GetSubTask(int taskId)
        {
            _manager.StoredProcedureName = SP_GET_SUB_TASK;
            _manager.AddInParameter("@SubTaskId", DbType.Int32, taskId);


            ISubTask subTask = new SubTask();
            Convert(subTask,_manager.ExecuteDataSet());
            return subTask;
        }
    }
}
